package com.example.administrator.leilihong;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SharePreferencesActivity extends AppCompatActivity {
    private EditText usernameET,passwordET;
    private Button login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_preferences);
        usernameET = findViewById(R.id.et_username);
        passwordET = findViewById(R.id.et_PSW);
        login = findViewById(R.id.bt_LOGIN);

        /***实现自动登录功能*/
        final SharedPreferences sp = getSharedPreferences("name",MODE_PRIVATE);
        final String username = sp.getString("username","name");//获取帐户信息
        String password = sp.getString("password","pass");//获取密码

                login.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    //手动输入密码,并存储密码
                        String in_username = usernameET.getText().toString();
                        String  in_psw = passwordET.getText().toString();
                        SharedPreferences.Editor editor = sp.edit();
                        if (in_username.equals("name")&&in_psw.equals("pass")){
                            editor.putString("username",in_username);
                            editor.putString("password",in_psw);
                            editor.commit();
                            Intent intent = new Intent(SharePreferencesActivity.this,SuccessActivity.class);
                            startActivity(intent);
                            Toast.makeText(SharePreferencesActivity.this,"已经保存密码",Toast.LENGTH_LONG).show();

                        }else {
                            Toast.makeText(SharePreferencesActivity.this,"帐户密码错误",Toast.LENGTH_LONG).show();

                        }
                    }
                });
        }


    }

